﻿using AutoMapper;
using MediatR;
using U.B.DoMain;
using U.B.ErrorCode;
using U.B.Repistory;
using U.B.WriteApi.Command;

namespace U.B.WriteApi.CommandHander
{
    public class AddurCommandHander : IRequestHandler<AdduserroleCommand, ApiResult<object>>
    {
        private readonly IRepistory<UserRole> _userRoleRepository;
        private readonly IMapper _mapper;
        public AddurCommandHander(IRepistory<UserRole> userRoleRepository, IMapper mapper)
        {
            _userRoleRepository = userRoleRepository;
            _mapper = mapper;
        }
        public Task<ApiResult<object>> Handle(AdduserroleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();
            foreach (var item in request.UserIds)
            {
                var ur = new UserRole();
                ur.UserId = item;
                ur.RoleId = request.RoleId;
                _userRoleRepository.Add(ur);
            }
            result.code = Renum.添加成功;
            result.Message = Renum.添加成功.ToString();
            return Task.FromResult(result);
        }
    }
}
